package com.jackrain.nea.fc.dl.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsPurOrderQueryCmd;
import com.jackrain.nea.fc.dl.mapper.WmsPurOrderMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 采购退货单->FC
 * @author xuxinshuai
 * @date 2018/05/05
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsPurOrderQueryCmdImpl extends CommandAdapter implements WmsPurOrderQueryCmd {

    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Autowired
    QueryCpUtil postQueryEvemt;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        WmsPurOrderMapper wmsPurOrderMapper = ApplicationContextHandle.getBean(WmsPurOrderMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        log.info("WmsPurOrderQueryCmdImpl-->param-->{}",param);
        JSONArray dataArray = param.getJSONArray("IDS");
        JSONArray fixArray = new JSONArray();

        for (int i = 0; i < dataArray.size(); i++) {

            JSONObject fixcolumn = new JSONObject();
            String objid = dataArray.getString(i);
            //查询主表
            HashMap mainMap = wmsPurOrderMapper.selectwmsordersql(objid);
            JSONObject mainJo = new JSONObject(mainMap);
            String status = mainJo.getString("STATUS");
            if (!"2".equals(status)) {
                throw new NDSException("不是已提交状态！");
            }
            mainJo.remove("STATUS");
            //跨库查询
            Long storeid = mainJo.getLong("wms_store_code");
            if (null != storeid) {

                JSONObject whereJo = new JSONObject();
                whereJo.put("ID", storeid);
                JSONObject result = postQueryEvemt.queryCP("CP_C_STORE", "WMS_STORE_ECODE,ISWMS", whereJo);

                if (result != null) {
                    String wmsecode = TypeUtils.castToString(result.get("WMS_STORE_ECODE"));
                    String iswms = TypeUtils.castToString(result.get("ISWMS"));
                    if (!"1".equals(iswms)) {

                        vh.put("code", 2);
                        vh.put("message", Resources.getMessage("对应店仓不是WMS管控!"));
                        return vh;
//                        throw new NDSException("对应店仓不是WMS管控！");
                    }
                    mainJo.put("wms_store_code", wmsecode);
                } else {
                    mainJo.put("wms_store_code", null);
                }
            }
            fixcolumn.put("mainData", mainJo);
            List<HashMap> mapList = wmsPurOrderMapper.selectwmsordertimesql(objid);

            JSONArray jsonArray = new JSONArray();
            for (int a = 0; a < mapList.size(); a++) {
                HashMap hashMap = (HashMap) mapList.get(a);
                JSONObject jsonObject = new JSONObject(hashMap);
                jsonObject.put("bill_no", mainJo.getString("bill_no"));
                jsonArray.add(jsonObject);
            }
            fixcolumn.put("itemData", jsonArray);
            fixArray.add(fixcolumn);
        }
        vh.put("data", fixArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("成功"));
        return vh;
    }

    /**
     * list TO jsonarra
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }

    /**
     * str TO Date
     *
     * @param str
     * @return Date
     */
    public Date stringToDate(String str) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        Date mdate = null;
        try {
            mdate = sdf.parse(str);

        } catch (Exception e) {
            throw new NDSException(e.getMessage());
        }
        return mdate;
    }

}